//
//  CNavBar.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/1.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "CNavBar.h"
#define CNavBar_Margin_LEFT 10
#define CNavBar_Margin_RIGHT 10
#define CNavBar_BtnFont 14

@interface CNavBar()
@property (nonatomic, strong) UIView *bottomLineView;
@property (nonatomic, strong) UILabel *lblBackTitle;
@property (nonatomic, strong) UIView *btnBackView;
@end

@implementation CNavBar

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void) initView{
    [self reloadView];
}

- (void)setBgColor:(UIColor *)bgColor{
    _bgColor = bgColor;
    if (CGColorEqualToColor(bgColor.CGColor, [UIColor whiteColor].CGColor)) {
        [self addSubview:self.bottomLineView];
    }else{
        [self.bottomLineView removeFromSuperview];
    }
    [self setBackgroundColor:bgColor];
}
- (void) createRightBtn:(NSString *) title block:(void(^)())block{
    CGSize size = [title sizeWithFont:F(CNavBar_BtnFont) constrainedToSize:CGSizeMake(100, 30)];
    UILabel *btn = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, MAX(100, size.width), 30)];
    [self setRightView:btn];
    [btn setText:title];
    [btn setFont:F(CNavBar_BtnFont)];
    [btn setTextColor:MainColor];
    [btn setTextAlignment:NSTextAlignmentRight];
    [btn setUserInteractionEnabled:YES];
    [btn bk_whenTapped:^{
        if (block) {
            block();
        }
    }];
}
- (void) createRightBtnWithImage:(UIImage *) image block:(void(^)())block{
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAX(50, image.size.width), MAX(30, image.size.height))];
    UIImageView *btnRight = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [rightView addSubview:btnRight];
    [btnRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rightView);
        make.right.equalTo(rightView);
        make.size.mas_equalTo(image.size);
    }];
    [btnRight setImage:image];
    self.rightView = rightView;
    [rightView bk_whenTapped:^{
        if (block) {
            block();
        }
    }];
}
- (void) createLeftBtn:(NSString *) title block:(void(^)())block{
    CGSize size = [title sizeWithFont:F(CNavBar_BtnFont) constrainedToSize:CGSizeMake(100, 30)];
    UILabel *btn = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, MAX(100, size.width), 30)];
    [self setLeftView:btn];
    [btn setText:title];
    [btn setFont:F(CNavBar_BtnFont)];
    [btn setTextColor:MainColor];
    [btn setTextAlignment:NSTextAlignmentLeft];
    [btn setUserInteractionEnabled:YES];
    [btn bk_whenTapped:^{
        if (block) {
            block();
        }
    }];
}


- (void) createLeftBtnWithImage:(UIImage *) image block:(void(^)())block{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAX(50, image.size.width), MAX(44, image.size.height))];
    UIImageView *btnLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [leftView addSubview:btnLeft];
    [btnLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftView);
        make.left.equalTo(leftView);
        make.size.mas_equalTo(image.size);
    }];
    [btnLeft setImage:image];
    self.leftView = leftView;
    [leftView bk_whenTapped:^{
        if (block) {
            block();
        }
    }];
}
- (UIView *)bottomLineView{
    if (_bottomLineView == nil) {
        _bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-.5, [UIScreen mainScreen].bounds.size.width, .5)];
        [_bottomLineView setBackgroundColor:LineColor];
    }
    return _bottomLineView;
}

- (void)setBackTitle:(NSString *)backTitle{
    if ([backTitle isEqualToString:@""]) {
        self.leftView = nil;
    }else{
        [self.lblBackTitle setText:backTitle];
        self.leftView = self.btnBackView;
    }
    [self reloadView];
}

- (void)setLeftView:(UIView *)leftView{
    _leftView = leftView;
    [self reloadView];
}

- (void)setRightView:(UIView *)rightView{
    _rightView = rightView;
    [self reloadView];
}

- (void)setTitleView:(UIView *)titleView{
    _titleView = titleView;
    [self reloadView];
}

- (void) reloadView{
    for (UIView *view in [self subviews]) {
        [view removeFromSuperview];
    }
    
    float width = [UIScreen mainScreen].bounds.size.width;
    if (self.leftView) {
        [self addSubview:self.leftView];
        [self.leftView setFrame:CGRectMake(CNavBar_Margin_LEFT, 20, self.leftView.frame.size.width, self.leftView.frame.size.height)];
    }
    if (self.rightView) {
        [self addSubview:self.rightView];
        [self.rightView setFrame:CGRectMake(width - self.rightView.frame.size.width - CNavBar_Margin_RIGHT, 20 + 22 - self.rightView.frame.size.height / 2, self.rightView.frame.size.width, self.rightView.frame.size.height)];
    }
    
    if (self.titleView) {
        [self addSubview:self.titleView];
        [self.titleView setCenter:CGPointMake(width / 2 , self.frame.size.height / 2 + 10)];
    }else{
        [self addSubview:self.lblTitle];
        float titlespace = MAX(self.leftView.frame.size.width + CNavBar_Margin_LEFT, self.rightView.frame.size.width + CNavBar_Margin_RIGHT);
        [self.lblTitle setFrame:CGRectMake(titlespace + CNavBar_Margin_LEFT, 20, width - 2 * titlespace, 44)];
        [self.lblTitle setCenter:CGPointMake(width / 2 , self.frame.size.height / 2 + 10)];
    }
    [self setBgColor:self.bgColor ? self.bgColor : [UIColor whiteColor]];
}

- (void)setTitle:(NSString *)title{
    [self.lblTitle setText:title];
    [self.lblTitle setTextAlignment:NSTextAlignmentCenter];
    [self reloadView];
}

- (UILabel *)lblBackTitle{
    if (_lblBackTitle == nil) {
        _lblBackTitle = [[UILabel alloc] init];
        [_lblBackTitle setText:@"返回"];
        [_lblBackTitle setTextColor:[UIColor whiteColor]];
        [_lblBackTitle setFont:F(14)];
        [self.btnBackView addSubview:_lblBackTitle];
    }
    return _lblBackTitle;
}

- (UIView *)btnBackView{
    if (_btnBackView == nil) {
        _btnBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
        [_btnBackView setUserInteractionEnabled:YES];
        [_btnBackView bk_whenTapped:^{
            if (self.BackAction) {
                self.BackAction();
            }
        }];
        UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];
        UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, icoBack.size.width, icoBack.size.height)];
        [backView setTag:100];
        [backView setImage:icoBack];
        [_btnBackView addSubview:backView];
        [backView setCenter:CGPointMake(icoBack.size.width / 2, _btnBackView.frame.size.height / 2 )];
        
        [_btnBackView addSubview:self.lblBackTitle];
        [self.lblBackTitle setFrame:CGRectMake(backView.frame.size.width + 5, 0, 50 - backView.frame.size.width, _btnBackView.frame.size.height)];
    }
    return _btnBackView;
}

- (void)setBackImage:(UIImage *)backImage{
    UIImageView *backImageView = [self.btnBackView viewWithTag:100];
    [backImageView setImage:backImage];
}
- (void)setBackColor:(UIColor *)backColor{
    [self.lblBackTitle setTextColor:backColor];
}

- (UILabel *)lblTitle{
    if (_lblTitle == nil) {
        _lblTitle = [[UILabel alloc] init];
        [self addSubview:_lblTitle];
        [_lblTitle setTextAlignment:NSTextAlignmentCenter];
        [_lblTitle setNumberOfLines:1];
        [_lblTitle setFont:F(16)];
        [_lblTitle setTextColor:[UIColor whiteColor]];
    }
    return _lblTitle;
}

@end
